package net.kindroid.fm;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.View;

/**
 * scroll View
 * 
 * @author huasong.chai
 * 
 */
public class ScrollView extends View
{
	public int left;
	public int top;
	public int right;
	public int bottom;
	public int size = 0;
	public int startMargin;
	public int offsetX;
	public int screen_height;
	public int total_line;
	public float scroll_bg_height_matrix;
	public float scroll_bg_width_matrix;
	public float line_height_matrix;
	public float line_width_matrix;
	public float line_margin_top;
	Bitmap scroll_bitmap;
	Bitmap pointer_bitmap;
	int startX;
	int black_line_height;
	int scroll_height;
	float line_margin;

	public ScrollView(Context context)
	{
		super(context);
	}

	public ScrollView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		scroll_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.scroll);
		pointer_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.scroll_pointer);
	}

	public void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);

		right = (int) (line_margin * (total_line + 2) + left + startMargin);
		drawBackground(canvas);
		drawLine(canvas);

	}

	private void drawBackground(Canvas canvas)
	{
		canvas.drawBitmap(scroll_bitmap, getBackgroundMatrix(), null);
	}

	private void drawLine(Canvas canvas)
	{
		for (int i = 0; i < total_line; i++)
		{
			startX = (int) (left + startMargin + offsetX + i * line_margin);
			if (startX > (right - startMargin))
			{
				startX = (int) (left + startMargin + left + startMargin + offsetX + i * line_margin - (right - startMargin));
			}
			if (startX < (left + startMargin))
			{
				startX = (int) (right - startMargin - (left + startMargin - (left + startMargin + offsetX + i * line_margin)));
			}
			if (startX >= (left + startMargin) && startX < (right - startMargin))
			{
				canvas.drawBitmap(pointer_bitmap, getLineMatrix(), null);
			}
			/**
			 * fix offset
			 */
			if (offsetX > 200)
			{
				offsetX = 0;
			} else if (offsetX < -200)
			{
				offsetX = 0;
			}
		}
	}

	/**
	 * scroll matrix
	 * 
	 * @return matrix
	 */
	private Matrix getLineMatrix()
	{
		Matrix matrix = new Matrix();
		float[] mf = new float[9];
		matrix.getValues(mf);
		mf[0] = line_width_matrix; 	// black line width
		mf[2] = startX; 			// black line start x
		mf[4] = line_height_matrix; // black line height
		mf[5] = line_margin_top; 	// black line margin top
		matrix.setValues(mf);
		return matrix;
	}

	/**
	 * scroll matrix
	 * 
	 * @return matrix
	 */
	private Matrix getBackgroundMatrix()
	{
		Matrix matrix = new Matrix();
		float[] mf = new float[9];
		matrix.getValues(mf);
		mf[0] = scroll_bg_width_matrix; 	//width
		mf[4] = scroll_bg_height_matrix; 	//height
		matrix.setValues(mf);
		return matrix;
	}
}
